
import wx
import wx.propgrid as wxpg

import ab_document
import ab_propgrid

import ab_ids as ai


class ConfigListDocFrameDocFrame(ab_document.DocumentFrame):
    def __init__(self, parent, app, hanlder, *args, **kws):
        # set the doc as a private one
        ab_document.DocumentFrame.__init__(self, parent, app, hanlder,
                                           'Config List')

        objc = app.readKey(ai.CfgParaConfigObject)
        self.prop = ab_propgrid.PropertyGrid(
            app, self, objc.getTemplate(), objc)

        sizer = wx.BoxSizer()
        sizer.Add(self.prop, 1, wx.EXPAND, 0)
        self.SetSizer(sizer)

        self.prop.Bind(wxpg.EVT_PG_CHANGED, self.OnPropGridChange)
        self.setReady()

    def OnPropGridChange(self, event):
        p = event.GetProperty()
        if p:
            name = p.GetName()
            value = p.GetValueAsString()

            va = self.prop.getValues(p.GetName(), p.GetValueAsString())
            if va:
                self.app.writeKey(va[0], va[1])

            if hasattr(self.parent, 'checkReboot'):
                self.parent.checkReboot()


class ConfigListDocFrameDocHandler(ab_document.DocumentHandler):
    def __init__(self):
        ab_document.DocumentHandler.__init__(self, 'cfglist',
                                             ConfigListDocFrameDocFrame,
                                             True, False)
